import axios from "axios"
import { request } from "@/utils/service"
import { getToken } from "@/utils/cache/cookies"
const baseURL = import.meta.env.VITE_BASE_API

/** 号卡销量查询 */
export function getNumberCard(params: any) {
  return request({
    url: "/sz/phoneNumber/queryData",
    method: "get",
    params,
    // 自定义参数序列化逻辑
    paramsSerializer: (params) => {
      const searchParams = new URLSearchParams()
      Object.entries(params).forEach(([key, value]) => {
        if (Array.isArray(value)) {
          // 数组处理：重复追加同名键
          if (value.length === 0) {
            searchParams.append(key, "")
          } else {
            value.forEach((item) => {
              searchParams.append(key, item.toString())
            })
          }
        } else {
          // 非数组直接追加
          searchParams.append(key, value.toString())
        }
      })
      return searchParams.toString()
    }
  })
}

/** 号卡销量Excel下载 */
export function downloadDataRequest(params: any) {
  return axios({
    url: `${baseURL}/sz/phoneNumber/downExcel`,
    method: "get",
    headers: {
      "Content-Type": "multipart/form-data",
      token: getToken()
    },
    params,
    // 自定义参数序列化逻辑
    paramsSerializer: (params) => {
      const searchParams = new URLSearchParams()
      Object.entries(params).forEach(([key, value]) => {
        if (Array.isArray(value)) {
          // 数组处理：重复追加同名键
          if (value.length === 0) {
            searchParams.append(key, "")
          } else {
            value.forEach((item) => {
              searchParams.append(key, item.toString())
            })
          }
        } else {
          // 非数组直接追加
          searchParams.append(key, value.toString())
        }
      })
      return searchParams.toString()
    },
    responseType: "blob"
  })
}
